$(document).ready(function() {
	$(".userinfo").fadeIn(3000);
	
	$("#homepage").click(function() {
		$('.sectionPanel:not(.infoboard)').hide();
		$('.infoboard').fadeIn(900);
	});
	
	
	$('#mcompose').click(function() {
		$('.sectionPanel:not(.mcompose)').hide();
		$('.mcompose').fadeIn(900);
	});
	
	$("#bdiscard").click(function() {
		if ( confirm("Are you sure?") ) {
			$('.sectionPanel:not(.infoboard)').hide();
			$('.infoboard').fadeIn(900);
		}
	});
	
	$('#manage').click(function() {
		$('.sectionPanel:not(.mmanage)').hide();
		$('.mmanage').fadeIn(900);
	});

	$('#allmail').click(function() {
		$('.sectionPanel:not(.home)').hide();
		$('.home').fadeIn(900);
	});
	
	$('#draft').click(function() {
		$('.sectionPanel:not(.home)').hide();
		$('.home').fadeIn(900);	
	});
	
	$('#trash').click(function() {
		$('.sectionPanel:not(.home)').hide();
		$('.home').fadeIn(900);	
	});

	$('#manGroups').click(function() {
		$('.manaPanel:not(.groupsPanel)').hide();
		$('.groupsPanel').fadeIn(900);
	});
	
	$('#addGroup').click(function() {
		$('.manaPanel:not(.addGroup)').hide();
		$('.addGroup').fadeIn(900);
	});
	
	$('#manContacts').click(function() {
		$('.manaPanel:not(.contactsPanel)').hide();
		$('.addGroup').hide();
		$('.contactsPanel').fadeIn(900);
	});
	

	function confsize(){
		var h_base = window.innerheight - 56;
		var h1 = $('.col1').height();
		var h2 = $('.col2').height();
		var h3 = $('.col3').height();
		$('.col1').height( Math.max(h_base, h1, h2, h3) );
	}
	confsize();
	$(window).resize(function(){confsize});
	
	
	API_ROOT = 'http://localhost:5000';
	url_sendmail = '/mail/send';
	url_getmaillist = '/mail/getList';
	url_getgrouplist = '/group/getList';
	url_getGroupMemberList = '/group/member/getList';
	url_mailRemove = '/mail/remove';
	url_groupAdd = '/group/add';
	
	$('#mailsend').click(function(){
		if ( $('#mearea textarea').val() != '' ) {
			var sender = $('#sender').val();
			var recipient = $('#recipient').val();
			var subject = $('#subject').val();
			var mcontent = $('#mcontent').val();
			$.post(url_sendmail, {
				'sender': sender,
				'recipient': recipient,
				'subject': subject,
				'mcontent': mcontent
			},function(data){
				alert('successful');
			});
		}//end if
	});
/* Show New Mails @ infoboard */	
	$.getJSON(url_getmaillist, function(data) {
		$.each(data.mails, function(index, mail){
			var li_html = '<li id="' + mail.mail_id + '">';
			li_html += '<h3><a href="#">' + mail.subject + '</a></h3>';
			li_html += '<span>' + mail.timestamp + '</span>';
			li_html += '<p>' + mail.content + '</p>';
			li_html += '</li>';
			$('#newMail').append( li_html );
		});
	});
/* Show All Groups @ sidePanel*/	
	$.getJSON(url_getgrouplist, function(data) {
		$.each(data.groups, function(index, group) {
			var list_group = '<li id="' + group.id + '">';
			list_group += '<a href="#">' + group.name +'</a>';
			list_group += '</li>';
			$('#mailgroup').append( list_group );
		});
	});
/* Show ALL Mail @ sidePanel */	
	$('#allmail').click(function() {
		$.getJSON(url_getmaillist, function(data) {
			$.each(data.mails, function(index, mail){
				var li_html = '<li class="msummary" id="' + mail.mail_id + '">';
				li_html += '<h3><a href="#">' + mail.subject + '</a></h3>';
				li_html += '<span>' + mail.timestamp + '</span>';
				li_html += '<p>' + mail.content + '</p>';
				li_html += '</li>';
				$('#newMail').append( li_html );
			});	
		});
/* Show Mail content */		
		$.getJSON(url_getmaillist, function(data) {
			$.each(data.mails, function(index, mail) {
				var content_mail = '<div class="con' + mail.mail_id + '">';
				content_mail += mail.subject + '</h1>';
				content_mail += '<p id="' + mail.mail_id + '>';
				content_mail +=	mail.content + '</p>' + '</div>';
				$('#mail-content').append( content_mail );
			});	
		});
	});

	$('.msummary').live('click', function(){
		var mail_id = $(this).attr('id');
		$( '.con' + mail_id ).show(fast);	
	});

/* Remove Mail */
	$('#mailRemove').click(function() {
		if ( confirm("Are you sure?") ) {
			var mail_id = $('#mail-content p').attr('id');
			$.post(url_mailRemove, {'mail_id': sender}, function(data) {
				alert('Mail Removed!');
			});
			$('.col2 .listB #' + mail_id ).remove();
			$('.mailbox .con' + mail_id ).remove();
		}
	});

	
	$('#manGroups').click(function() {
		$.getJSON(url_getgrouplist, function(data) {
			$.each(data.groups, function(index, group) {
				var list_group = '<li class="groupMemberId" id="' + group.id + '">';
				list_group += '<h3>'+'<a href="#">' + group.name +'</a>';
				list_group += '</h3>' + '<span>' + group.owner + '</span>';
				list_group += '<p>' + group.topic + '</p>' + '</li>';
				$('.collist2').append( list_group );
			});
		});	
		
		$('.groupMemberId').click(function() {
			$('.concol3').show();
			var group_id = $(this).attr('id');
			$.getJSON(url_getGroupMemberList, {'groupid':group_id}, function(data) {
				$.each(data.contacts, function(index,contact) {
					var list_member = '<li id="' + contact.user_id + '">';
					list_member += '<a href="#">' + contact.username + '</a>';
					list_member += '<span><' + contact.address + '></span>';
					list_member += '<p>' + contact.fullname + '</p>' + '</li>';
					$('.collist3').append( list_member );
				});
			});
		});	
	});

	$('#addGroupConfirm').click(function() {
		var name = $('#groupName').val();
		var topic = $('#groupTopic').val();
		var policy = $('#groupPolicy').val();
		$.post(url_groupAdd, {
			'name': name,
			'topic': topic,
			'policy': policy
			}, function(data) {
			alert('Success!');
		});
	});



	
	
	
	
	
	
	
	
	
	
	
});